<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Marketing;


use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Good;
use App\Models\JinGangAnew;
use App\Models\Order;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class JinGoodController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\Marketing\JinGoodController";

    public function index(Request $request)
    {
        $id = $request->id;
        $name = $request->name;
        $jin_id = $request->jin_id;
        $pageSize = $request->pageSize;
        $sql = Good::select('t1.id','t2.name as jin_name','t1.image','t1.name','t1.vip_price','t1.price','t1.jinIndex')
            ->from((new Good())->getTable() . ' as t1')
            ->join((new JinGangAnew())->getTable() . ' as t2', 't1.jingang_id', '=', 't2.id')
            ->where('t1.jingang_id', $jin_id);
        if (isset($id)) {
            $sql->where('t1.id', $id);
        }
        if (isset($name)) {
            $sql->where('t1.name', 'like', "%$name%");
        }
        $goodList = $sql->orderBy('t1.jinIndex', 'desc')
            ->orderBy('t1.jinIndexTime', 'desc')
            ->orderBy('t1.createtime', 'desc')
            ->paginate($pageSize)
            ->toArray();
        $data['goodsList'] = $goodList['data'];
        foreach ($data['goodsList'] as $key => $value) {
            $data['goodsList'][$key]['image'] = path($value['image']);
        }
        $data['totalNum'] = $goodList['total'];
        return $this->apiResponse($data);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $jin_id = $request->jin_id;
        $jin = JinGangAnew::where('id',$jin_id)->first();
        if(!$jin){
            return $this->apiResponse('', config('errorLiu.JINGANGQUBUCUNZAII'));
        }
        $good = Good::where('id',$id)->where('status',1)->where('is_del',0)->first();
        if(!$good){
            return $this->apiResponse('', config('errorLiu.SHANGPONINBUCUNAZAIDEHI'));
        }
        if($good['jingang_id']){
            return $this->apiResponse('', config('errorLiu.GAISHANGPINYICUNZAIJINGANGQU'));
        }
        Good::where('id',$id)->where('status',1)->where('is_del',0)->update([
            'jingang_id'=>$jin_id
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = "新增";
        $content = "将id为【".$id."】的商品设置成id为【".$jin_id."】的金刚区下属";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "金刚区商品模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function destroy(Request $request,$id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $good = Good::select()->where('id', $id)
            ->first();
        Good::where('id', $id)->update([
            'jingang_id' => null,
            'jinIndex' => 0,
            'jinIndexTime' => date("Y-m-d H:i:s")
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了id为【".$good['jingang_id']."】的金刚区分类下id为【".$id."】的商品";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "金刚区商品模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function update(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        Good::where('id', $id)->update([
            'jinIndex' => 1,
            'jinIndexTime' => date("Y-m-d H:i:s")
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/update";
        $title = "修改";
        $content = "将id为【".$id."】的金刚区商品置顶";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "金刚区商品模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
//        $good = Good::where('id', $id)->first();
//        if ($good['jinIndex']) {
//            Good::where('id', $id)->update([
//                'jinIndex' => 0,
//                'jinIndexTime' => null
//            ]);
//        } else {
//            Good::where('id', $id)->update([
//                'jinIndex' => 1,
//                'jinIndexTime' => date("Y-m-d H:i:s")
//            ]);
//        }
        return $this->apiResponse(true);
    }

}